<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.company.DepartmentComInfo" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%
    JSONObject dataJson = new JSONObject();
    String bm = Util.null2String(request.getParameter("bm"));
    String yf = Util.null2String(request.getParameter("yf"));
    String sybus = getAllSybu();
    String[] sybusArr = sybus.split(",");
    try {
        DepartmentComInfo departmentComInfo = new DepartmentComInfo();
        ResourceComInfo resourceComInfo = new ResourceComInfo();
        //发票挂账
        JSONArray fpgzList = new JSONArray();
        JSONArray fybxList = new JSONArray();
        JSONArray clfbxList = new JSONArray();
        rs.executeQuery("select ry,gys,ysfymc,hjkm,hzje,xglc,ryjdbm,bmjdbm,lclx from uf_fttzdb where lclx in (0,1,2) and bm=? and ny=?", bm, yf);
        while (rs.next()) {
            String ry = Util.null2String(rs.getString("ry"));
            String gys = Util.null2String(rs.getString("gys"));
            String ysfymc = Util.null2String(rs.getString("ysfymc"));
            String hjkm = Util.null2String(rs.getString("hjkm"));
            String hzje = Util.null2String(rs.getString("hzje"));
            String xglc = Util.null2String(rs.getString("xglc"));
            String ryjdbm = Util.null2String(rs.getString("ryjdbm"));
            String bmjdbm = Util.null2String(rs.getString("bmjdbm"));
            String lclx = Util.null2String(rs.getString("lclx"));
            for (String sybu : sybusArr) {
                JSONObject jsonData = new JSONObject();
                jsonData.put("ry", ry);
                jsonData.put("ryname", resourceComInfo.getLastname(ry));
                jsonData.put("gys", gys);
                jsonData.put("gysname", getGysbmc(gys));
                jsonData.put("ysfymc", ysfymc);
                jsonData.put("ysfymcname", getYsfymc(ysfymc));
                jsonData.put("hjkm", hjkm);
                jsonData.put("hzje", hzje);
                jsonData.put("xglc", xglc);
                jsonData.put("ryjdbm", ryjdbm);
                jsonData.put("bmjdbm", bmjdbm);
                jsonData.put("sybu", sybu);
                jsonData.put("sybuname", departmentComInfo.getDepartmentName(sybu));
                if ("0".equals(lclx)) {
                    fpgzList.add(jsonData);
                }
                if ("1".equals(lclx)) {
                    fybxList.add(jsonData);
                }
                if ("2".equals(lclx)) {
                    clfbxList.add(jsonData);
                }
            }
        }
        dataJson.put("fpgzList", fpgzList);
        dataJson.put("fybxList", fybxList);
        dataJson.put("clfbxList", clfbxList);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(dataJson.toJSONString());
%>
<%!
    private static String getAllSybu() {
        RecordSet rs = new RecordSet();
        String sybus = "";
        rs.executeQuery("select sybu from uf_sybu");
        while (rs.next()) {
            String sybu = Util.null2String(rs.getString("sybu"));
            sybus += "".equals(sybus) ? sybu : "," + sybu;
        }
        return sybus;
    }

    private static String getYsfymc(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select name from fnabudgetfeetype where id=?", id);
        rs.next();
        return Util.null2String(rs.getString("name"));
    }

    private static String getGysbmc(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select gysmc from uf_gysb where id=?", id);
        rs.next();
        return Util.null2String(rs.getString("gysmc"));
    }

%>